var ctxPath = "/education";

!function ($) {
    var orderCode = $.getUrlParam('orderCode');
    var phone;
    /**
     * 初始化游学产品详情方法
     * @param id
     */
    var initOrderDetail = function(id) {
        $.ajax({
            url: ctxPath + '/wx/api/order/getOrderByCode',
            type: 'post',
            dataType: 'json',
            data: {orderCode: orderCode},
            success: function (result) {
                if(result.returnCode == 0) {

                    $("#orderCode").html("订单编号: " + result.orderCode);
                    $("#productName").html(result.productName);

                    $("#applicantName").html("报名人姓名: " + result.applicantName);
                    $("#groupName").html("参团姓名: " + result.groupName);
                    $("#phone").html("联系方式: " + result.phone);
                    phone = result.phone;
                    $("#schoolGradeClazz").html("学校/年级/班级: " + result.schoolGradeClazz);

                    $(".list-price").html("<em>¥</em>" + result.productDeposit);

                    $("#showImage").attr("src", result.showImage);

                } else {
                    alert(result.returnMsg);
                }
            }
        });
    };

    initOrderDetail(orderCode);

    $("#payBtn").click(function () {
        $.ajax({
            url: ctxPath + '/wx/api/jspay',
            type: 'post',
            dataType: 'json',
            data: {orderCode: orderCode},
            success: function (result) {
                wx.ready(function() {
                    wx.chooseWXPay({
                        appId: result.appId,
                        timestamp: result.timeStamp,
                        nonceStr: result.nonceStr,
                        package: result.pkg, // 统一支付接口返回的prepay_id参数值，提交格式如：prepay_id=***）
                        signType: result.signType,
                        paySign: result.paySign, // 支付签名
                        success: function(res) {
                            // 支付成功后的回调函数
                            if (res.errMsg == "chooseWXPay:ok") {
                                //支付成功
                                location.href = "/education/wx/paySuccess.html?phone=" + phone;
                                alert('支付成功');
                            } else {
                                alert(res.errMsg);
                            }
                        },
                        cancel: function(res) {
                            //支付取消
                            alert('支付取消' + res.errMsg);
                        },

                        fail:function (res) {
                            //支付失败
                            alert('支付取消' + res.errMsg);
                        }
                    });
                });
            }
        });
    });
}(jQuery);